package com.ruoyi.crm.controller;

import java.util.List;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.crm.domain.CrmOrderDetails;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.crm.domain.CrmOrders;
import com.ruoyi.crm.service.ICrmOrdersService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 订单信息Controller
 * 
 * @author huRitao
 * @date 2021-10-14
 */
@RestController
@RequestMapping("/crmOrders")
public class CrmOrdersController extends BaseController
{
    /**
     * @RestController 相当于 @Controller和 @ResponseBody 的结合体
     * @Controller 表示该类是一个控制类
     * @ResponseBody 返回数据到响应体中，以Json格式输出
     * @Resource 实体类bean注入，可写在属性和setter方法上
     */
    @Resource
    private ICrmOrdersService crmOrdersService;

    /**
     * 查询订单信息列表
     */
    @PreAuthorize(hasPermi = "crm:crmOrders:list")
    @GetMapping("/list")
    public TableDataInfo list(CrmOrders crmOrders)
    {
        startPage();
        List<CrmOrders> list = crmOrdersService.selectCrmOrdersList(crmOrders);
        return getDataTable(list);
    }

    /**
     * 导出订单信息列表
     */
    @PreAuthorize(hasPermi = "crm:crmOrders:export")
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmOrders crmOrders) throws IOException
    {
        List<CrmOrders> list = crmOrdersService.selectCrmOrdersList(crmOrders);
        ExcelUtil<CrmOrders> util = new ExcelUtil<CrmOrders>(CrmOrders.class);
        util.exportExcel(response, list, "订单信息数据");
    }

    /**
     * 获取订单信息详细信息
     */
    @PreAuthorize(hasPermi = "crm:crmOrders:query")
    @GetMapping(value = "/{orderUuid}")
    public AjaxResult getInfo(@PathVariable("orderUuid") String orderUuid)

    {
        return AjaxResult.success(crmOrdersService.selectCrmOrdersByOrderUuid(orderUuid));
    }

    /**
     * 新增订单信息
     */
    @PreAuthorize(hasPermi = "crm:crmOrders:add")
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmOrders crmOrders)
    {
        CrmOrderDetails crmOrderDetails=new CrmOrderDetails();
        crmOrderDetails.setOrderUuid(crmOrders.getOrderUuid());
        if(crmOrders.getCrmOrderDetailsList().size()>0) {
            int a=crmOrdersService.insertCrmOrders(crmOrders);
            return toAjax(a);
        }else{
            return new AjaxResult(1501,"新增失败,请添加订单明细,至少需要一条订单明细！");
        }

    }

    /**
     * 修改订单信息
     */
    @PreAuthorize(hasPermi = "crm:crmOrders:edit")
    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmOrders crmOrders)
    {
        CrmOrderDetails crmOrderDetails=new CrmOrderDetails();
        crmOrderDetails.setOrderUuid(crmOrders.getOrderUuid());
        if(crmOrders.getCrmOrderDetailsList().size()>0) {
            return toAjax(crmOrdersService.updateCrmOrders(crmOrders));
        }else{
            return new AjaxResult(1521,"修改失败,请添加订单明细,至少需要一条订单明细！");
        }
    }

    /**
     * 修改订单信息
     */
    @PreAuthorize(hasPermi = "crm:crmOrders:edit")
    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
    @PutMapping("/submitConfirm")
    public AjaxResult submitConfirm(@RequestBody CrmOrders crmOrders)
    {
            return toAjax(crmOrdersService.submitConfirm(crmOrders));
    }

    /**
     * 删除订单信息
     */
    @PreAuthorize(hasPermi = "crm:crmOrders:remove")
    @Log(title = "订单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderUuids}")
    public AjaxResult remove(@PathVariable String[] orderUuids)
    {
        return toAjax(crmOrdersService.deleteCrmOrdersByOrderUuids(orderUuids));
    }
}
